﻿
using System.Collections;
using System.Xml;

namespace Xacml.Core.Context
{
  public class ResourceCollectionReadWrite : CollectionBase
  {
    public virtual ResourceElementReadWrite this[int index]
    {
      get
      {
        return (ResourceElementReadWrite) this.List[index];
      }
      set
      {
        this.List[index] = (object) value;
      }
    }

    public virtual int Add(ResourceElementReadWrite value)
    {
      return this.List.Add((object) value);
    }

    public new virtual void Clear()
    {
      base.Clear();
    }

    public new virtual void RemoveAt(int index)
    {
      base.RemoveAt(index);
    }

    public int GetIndex(ResourceElementReadWrite resource)
    {
      for (int index = 0; index < this.Count; ++index)
      {
        if (this.List[index] == resource)
          return index;
      }
      return -1;
    }

    public void WriteDocument(XmlWriter writer)
    {
      foreach (ResourceElementReadWrite elementReadWrite1 in (IEnumerable) this.List)
      {
        writer.WriteStartElement("Resource");
        if (elementReadWrite1.ResourceContent != null)
        {
          writer.WriteStartElement("ResourceContent");
          writer.WriteRaw(elementReadWrite1.ResourceContent.XmlDocument.InnerXml);
          writer.WriteEndElement();
        }
        foreach (AttributeElementReadWrite elementReadWrite2 in (CollectionBase) elementReadWrite1.Attributes)
        {
          writer.WriteStartElement("Attribute");
          writer.WriteAttributeString("AttributeId", elementReadWrite2.AttributeId);
          writer.WriteAttributeString("DataType", elementReadWrite2.DataType);
          if (elementReadWrite2.Issuer != null)
            writer.WriteAttributeString("Issuer", elementReadWrite2.Issuer);
          foreach (AttributeValueElementReadWrite elementReadWrite3 in (CollectionBase) elementReadWrite2.AttributeValues)
            writer.WriteElementString("AttributeValue", elementReadWrite3.Value);
          writer.WriteEndElement();
        }
        writer.WriteEndElement();
      }
    }
  }
}
